﻿using SQLServer.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Tenancy.Helper;
using WebUI;

namespace Tenancy.Server
{
    /// <summary>
    /// Hbung_UploadPhoto 的摘要说明
    /// </summary>
    public class Hbung_UploadPhoto : IHttpHandler
    {
        //上传头像接口
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //用户登录的手机号
            string Hbung_Tel = context.Request.Params["Tel"];
            string Hbung_Phone = string.Empty;
            if (string.IsNullOrEmpty(Hbung_Tel))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Tel为空"));
            }
            else
            {
                string pattern = "^1[0-9]{10}$"; //正则表达式字符串
                Regex cgsregex = new Regex(pattern);
                Hbung_Phone = DESEncrypt.MD5Decrypt(Hbung_Tel);
                bool isMatch = cgsregex.IsMatch(Hbung_Phone);
                if (!isMatch)
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Tel格式不正确"));
                    return;
                }
            }
            //签名
            string HBsign = context.Request.Params["Sign"];
            if (string.IsNullOrEmpty(HBsign))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Sign为空"));
                return;
            }
            else
            {
                if (GenerClass.IsOkMatch(Hbung_Phone) != Regex.Replace(HBsign, @"\s", "+"))
                {
                    context.Response.Write(GenerClass.SetParamErrorJsData("Sign不匹配"));
                    return;
                }
            }

            string img = "";//过程图片

            #region 上传头像
            if (HttpContext.Current.Request.Files["PicIO"] != null)
            {
                string[] img_guocheng_names = HttpContext.Current.Request.Files.AllKeys;
                for (int i = 0; i < img_guocheng_names.Length; i++)
                {
                    if (img_guocheng_names[i] == "PicIO")
                    {
                        //需要上传的图片
                        HttpPostedFile _upfile = HttpContext.Current.Request.Files[i];
                        if (_upfile.ContentLength > 0)
                        {
                            PicUload upFiles = new PicUload();
                            string msg = upFiles.fileSaveAs(_upfile);
                            Dictionary<string, string> dic_msg = new Dictionary<string, string>();
                            dic_msg = JsonHelper.JSONToObject<Dictionary<string, string>>(msg);
                            if (dic_msg["status"] == "1")
                            {
                                img += dic_msg["path"] + ",";
                            }
                        }
                    }
                }
                if (img.EndsWith(","))
                {
                    img = img.Remove(img.Length - 1, 1);

                }
                string StrPic = string.Format("update Hbung_CompanyInfo set PicUrl='{0}'  where CellTel='{1}' ", img, Hbung_Phone);
                if (DBHelpSQL.ExecuteSql(StrPic) > 0)
                {
                    string UserStr = string.Format("select PicUrl from Hbung_CompanyInfo where CellTel='{0}' ", Hbung_Phone);
                    DataTable HB_ComInfo = DBHelpSQL.Query(UserStr).Tables[0];
                    string Hb_DateJson = JsonHelper.DataTableToJSON(HB_ComInfo);
                    context.Response.Write(GenerClass.SetSucessJsData("上传成功", Hb_DateJson));
                    return;
                }
                else
                {
                    context.Response.Write(GenerClass.SetChangeErrorJsData("上传失败"));
                    return;
                }
            }

            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}